import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:home/assets/r.dart';
import 'package:home/net/network.dart';
import 'package:home/page/router.dart';
import 'package:home/utils/loger.dart';

import 'assets/theme.dart';

void main() {
  runApp(const MyApp());
}


class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      navigatorKey: StationRouter.navigatorKey,
      routes: StationRouter.routers,
      theme: getTheme(context: context),
      darkTheme: getTheme(context: context,isDark: true),
      builder: (context,child){
        return GestureDetector(
          onTap: (){
            FocusScopeNode currentFocus = FocusScope.of(context);
            if(!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null){
              logger.info("隐藏键盘");
              FocusManager.instance.primaryFocus?.unfocus();
            }
          },
          child: child,
        );
      },
      home: SplashPage(title: 'Flutter Demo Home Page'),
    );
  }
}

class SplashPage extends StatefulWidget {
  const SplashPage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<SplashPage> createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {

  @override
  void initState() {
    super.initState();
    NetWork.getInstance().hasLoginCookies().then((value) {
         if(!value){
           Navigator.pushReplacementNamed(context, StationRouter.login);
         }else{
           Navigator.pushReplacementNamed(context, StationRouter.home);
         }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Theme.of(context).scaffoldBackgroundColor,
      body: Center(
        child: Stack(
          children: [
            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Image.asset(R.drawable.splashBackground),
                const CupertinoActivityIndicator(),
                const Padding(
                  padding: EdgeInsets.only(top: 18),
                  child: Text("检查环境"),
                )
              ],
            )
          ],
        ),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
